package com.hn.shopadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hn.shopadmin.service.MenuService;
import com.hn.kiss.modules.sys.shopdb.entity.Menu;
import com.hn.kiss.modules.sys.shopdb.entity.MenuExample;
import com.hn.kiss.modules.sys.shopdb.mapper.MenuMapper;
import com.hn.shopadmin.dao.MenuDao;
import com.hn.shopadmin.dto.MenuDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MenuServiceimpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MenuDao menuDao;

    @Override
    public int addParent(Menu menu) {
        menu.setParentId("0");
        menu.setCreatDate(new Date());
        return menuMapper.insert(menu);
    }

    @Override
    public int addChild(Menu menu) {
        menu.setCreatDate(new Date());
        return menuMapper.insert(menu);
    }

    @Override
    public int update(Menu menu) {
        menu.setModifyDate(new Date());
        return menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public int del(String id) {
        int count = 0;

        List<Menu> menus = listChild(id);
        if (menus != null){

            for (Menu menu: menus) {
                List<Menu> menus1 = listChild(menu.getId());
                if (menus != null){
                    for (Menu menu1: menus1) {
                        delById(menu1.getId());
                    }
                    delById(menu.getId());
                }else{
                    delById(menu.getId());
                }
            }
            count = delById(id);
        }else{
            count = delById(id);
        }

        return count;
    }

    @Override
    public PageInfo<Menu> listPageParent(Integer page, Integer limit, String keywork) {
        PageHelper.startPage(page, limit);
        if (keywork == null){
            keywork = "%%";
        }else{
            keywork = "%" + keywork.trim() + "%";
        }

        MenuExample menuExample = new MenuExample();
        menuExample.createCriteria()
                .andParentIdEqualTo("0")
                .andNameLike(keywork);

        List<Menu> menus = menuMapper.selectByExample(menuExample);
        return new PageInfo<Menu>(menus);
    }

    @Override
    public PageInfo<MenuDTO> listPageHtml(Integer page, Integer limit, String keywork, String parentId) {
        PageHelper.startPage(page, limit);
        List<MenuDTO> list = menuDao.list(parentId, keywork);
        return new PageInfo<MenuDTO>(list);
    }

    @Override
    public List<MenuDTO> listButton(String parentId) {
        return menuDao.list(parentId, null);
    }

    /**
     * 通过父菜单id获取子菜单列表
     * @param parentId
     * @return
     */
    List<Menu> listChild(String parentId){
        MenuExample menuExample = new MenuExample();
        menuExample.createCriteria().andParentIdEqualTo(parentId);
        List<Menu> menus = menuMapper.selectByExample(menuExample);
        return menus;
    }

    /**
     * 通过id删除菜单
     * @param id
     * @return
     */
    int delById(String id){
        return menuMapper.deleteByPrimaryKey(id);
    }
}
